#include "SFC_Inc.hpp"
#include "SFC_Button.h"

SFC_Button::SFC_Button()
{
	TextChanged.Bind(Text);
	this->Children.push_back(&this->lab);
}

void SFC_Button::LoadRes()
{
	// Create cache surface
	if (this->Cache != nullptr)
		FreeSurface(this->Cache);
	this->Cache = CreateSurface(this->Size.w, this->Size.h);
	FillRect(this->Cache, nullptr, 0xFFFFFFFF);

	this->lab.Text = this->Text;
	this->lab.Size = {0, 0, this->Size.w, this->Size.h};
	this->lab.Transform = true;
}

void SFC_Button::EventRouter(SFC_Event &Event)
{
	if (Event.Type == EventType::Mouse && Event.Mouse.Type == MouseEventType::Up)
	{
		this->OnClick.ProcDrv();
	} else if (Event.Type == EventType::Window && Event.Window.Type == WindowEventType::Paint) {
		Paint();
	}
}

void SFC_Button::Paint()
{
	if (this->Durty == false) return ;

	LoadRes();
	// Draw background surface
	this->Cache = CreateSurface(this->Size.w, this->Size.h);
	FillRect(this->Cache, nullptr, 0xFFAAAAAA);
	// Draw frontground surface
	this->Durty = false;
}